<script>
	const redirect_uri = "https://home.jswbgz.com/h5/pages/Index/index"; //授权后重定向的回调链接地址
	const scope = "snsapi_base"; //非静默授权：snsapi_userinfo  静默授权：snsapi_base

	export default {
		onLaunch: function(e) {
			console.log('App Launch')
			console.log("getopenid 1")
			//console.log(this.$iswechat())
			//判断是否登录
			//this.$checkLogin(e.path)
			let iswechat = false;
			let ua = window.navigator.userAgent.toLowerCase();
			//alert(ua)
			if (ua.match(/MicroMessenger/i) == 'micromessenger') {
				iswechat = true;
			} else {
				iswechat = false;
			}
			//alert(iswechat)
			if (iswechat) {
				//alert(e.query.mini_openid);
	            if(e.query && e.query.mini_openid){
					//alert("小程序打开-"+e.query.mini_openid);
					uni.setStorageSync('mini_openid', e.query.mini_openid);
				}
				else{
					//uni.removeStorageSync('mini_openid');
				}
				const openid = uni.getStorageSync("OPENID");
				//alert(openid)
				//不存在code
				if (!(e && e.query && e.query.code) && !openid) {
					//获取code
					uni.request({
						url: "https://bg.jswbgz.com/post/weixin:get_oauth_url",
						data: {
							redirectUrl: redirect_uri,
							scope: scope
						},
						header: {
							'Content-Type': 'application/x-www-form-urlencoded'
						},
						dataType: 'json',
						method: 'post',
						success: (res) => {
							console.log(res.data.data.code)
							if (res.data.data.code == 1) {
								window.location.href = res.data.data.msg;

							} else {
								uni.showToast({
									title: res.data.data.msg,
									icon: "none"
								});
							}
						},
						fail: (res) => {
							uni.showToast({
								title: res.data.data.msg,
								icon: "none"
							});
							//console.log(res)
						}
					});

				}
				//console.log("getopenid 2")
				//alert(location.href)
				//alert(e.query.code)
				//alert(openid)
				if ((e && e.query && e.query.code) && !openid) {
					//请求后端接口获取用户的信息
					//保存用户的openid
					uni.request({
						url: "https://bg.jswbgz.com/post/weixin:get_open_id",
						data: {
							code: e.query.code
						},
						header: {
							'Content-Type': 'application/x-www-form-urlencoded'
						},
						dataType: 'json',
						method: 'post',
						success: (res) => {
							//console.log(res.data)
							//alert(res.data.data.msg)
							if (res.data.data.code == 1) {
								var json_res = JSON.parse(res.data.data.msg);
								//alert(json_res.openid)
								uni.setStorageSync('OPENID', json_res.openid);
							}

						},
						fail: (res) => {

							console.log(res)
						}
					});
				}

			}

		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		}
		
	}
</script>

<style lang="scss">
	@import '@/uni_modules/uni-scss/index.scss';
	.place {
		background-color: #ffffff;
		height: 130upx;
		/*  #ifdef  APP-PLUS  */
		margin-top: var(--status-bar-height);
		/*  #endif  */
	}
	/*每个页面公共css */
	@import "/static/iconfont/font.scss";
</style>
